package com.forlove.app.qselect.index.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.friend.entity.ContactsFriendEntity;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;
import com.forlove.app.qselect.index.service.IAddFriendsService;
import com.forlove.app.qselect.index.service.impl.AddFriendsServiceImpl;
import com.forlove.app.qselect.index.view.AddFriendsView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yinzhiqiang on 2017/11/6.
 */

public class AddFriendsPresenter extends IBasePresenter {
    private AddFriendsView addFriendsView;
    private IAddFriendsService addFriendsService;

    public AddFriendsPresenter(AddFriendsView addFriendsView) {
        this.addFriendsView = addFriendsView;
        addFriendsService = new AddFriendsServiceImpl();
        setService(addFriendsService);
    }

    public void requestAddFriends(List<MineFriendEntity> mineFriendEntities) {
        if (mineFriendEntities == null) {
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < mineFriendEntities.size(); i++) {
            MineFriendEntity mineFriendEntity = mineFriendEntities.get(i);
            if (StringUtils.isNotEmpty(mineFriendEntity.mobile)) {
                stringBuilder.append(mineFriendEntity.mobile);
            } else {
                stringBuilder.append(mineFriendEntity.contactsNumber);
            }
            if (i < mineFriendEntities.size() - 1) {
                stringBuilder.append(",");
            }
        }
        Map param = new HashMap();
        param.put("list", stringBuilder.toString());
        addFriendsService.loadData(ServerManager.ACTION_ADD_FRIENDS, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                addFriendsView.updateAddFriendView(commonEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    addFriendsView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    addFriendsView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

    /**
     * 获取通讯录注册好友
     */
    public void loadContactFriend() {
        addFriendsService.loadData(ServerManager.ACTION_GET_CONTACTS_FRIEND, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                ContactsFriendEntity friendListEntity = new ContactsFriendEntity();
                friendListEntity.parseJSONString(ParseHelper.getString(result, "data"));
                if (friendListEntity != null) {
                    addFriendsView.updateFriendListView(friendListEntity);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    addFriendsView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    addFriendsView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }
}
